package com.example.demo.servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Rocket
 * @version 10.0.1
 * @date 2021-08-08 10:56 AM
 */
@WebFilter(filterName = "authFilter", urlPatterns = {"/first", "/second"})
public class AuthFilter implements Filter {
    private String token = "xxxToken";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String userToken = req.getParameter(token);
        if(token.equals(userToken)) {
            filterChain.doFilter(servletRequest,servletResponse);
        } else {
            servletResponse.getWriter().println("access denied...");
        }

        return;
    }

    @Override
    public void destroy() {

    }
}
